using UnityEngine;
using System.Collections;

public struct Train
{
	public GameObject obj;
	public int dir;
	public int speed;
	public bool havePlayer;
	public float randomStop;
	public bool trainStopped;
	public float startTime;
	public bool isDriving;
}

public class TrainController : MonoBehaviour {
	public ArrayList trains;
	public Camera camera;
	int changeColors = 0;

	public float randomStop;
	public bool trainStopped;
	float startTime;

	// Use this for initialization
	void Start () 
	{
		randomStop = Random.Range(100, Screen.width - 200);

		trains = new ArrayList();

		for (int i=0; i<4; ++i)
		{
			Train train = new Train();

			train.obj = GameObject.Find("train" + (i+1));
			train.dir = 1;
			train.speed = 100;
			train.havePlayer = false;
			train.randomStop = Random.Range(100, Screen.width - 200);
			train.trainStopped = false;
			train.startTime = 0;
			train.isDriving = true;

			trains.Add(train);
		}
	}
	
	// Update is called once per frame
	void Update () {
		UpdateTrain();
		//Debug.Log(GUISpalvos.generavimoMasyvas.ToString());
	}

	void UpdateTrain()
	{
		// If train is inside screen - update position
		// if train is outside screen - change color, wait few seconds, go to other direction

		changeColors = 0;
		for (int i=0; i<trains.Count; ++i)
		{
			Train train = (Train) trains[i];

	//		train = isStopped(train);
			//if (isStopped(train)) {
			//	Debug.Log("is stopped " + i);
			//	continue;
			//}

	//		if (train.trainStopped)
	//			continue;

			Vector3 pos = train.obj.transform.position;

			if (((train.dir > 0 && pos.x > randomStop) || (train.dir < 1 && pos.x < randomStop)))
			{
				if (!trainStopped)
				{
					trainStopped = true;
					startTime = Time.time + 3;
					continue;
				}
				else if (Time.time < startTime)
					continue;
				//else
				//	trainStopped = false;

			}

		//	GameObject train = tr.obj;

			if (InsideScreen(train.obj))
			{
				train.obj.transform.Translate(train.dir * Time.deltaTime * train.speed, 0, 0);
			}
			else 
			{
				randomStop = Random.Range(100, Screen.width - 200);
				trainStopped = false;
				//train.obj.renderer.material.mainTexture = (Texture) GUISpalvos.generavimoMasyvas[i];
				train.dir *= -1;
				train.obj.transform.Translate(train.dir * Time.deltaTime * 2 * train.speed, 0, 0);
				++changeColors;
			}

			trains[i] = train;
		}

		GUISpalvos.NaujaSpalva(changeColors);
		for (int i=0; i<changeColors; ++i)
			((Train)trains[i]).obj.renderer.material.mainTexture = (Texture) GUISpalvos.generavimoMasyvas[i];
	}


	Train isStopped(Train train)
	{
		Vector3 pos = train.obj.transform.position;
		//train.isDriving = true;

		if (((train.dir > 0 && pos.x > train.randomStop) || (train.dir < 1 && pos.x < train.randomStop)))
		{
			if (!train.trainStopped)
			{
				train.trainStopped = true;
				train.startTime = Time.time + 3;
				train.isDriving = false;
			}
			else if (Time.time < train.startTime)
				train.isDriving = false;
			else 
			{
				train.randomStop = Random.Range(100, Screen.width - 200);
				//train.trainStopped = false;
				train.isDriving = true;
			}
		}
		//train.isDriving = true;
		return train;
	}


	bool InsideScreen(GameObject obj)
	{
		//Debug.Log(tex.transform.position.ToString());
		Vector3 worldPosMax = new Vector3(obj.renderer.bounds.max.x, obj.renderer.bounds.max.y, 0);
		Vector3 screenPosMax = camera.WorldToScreenPoint(worldPosMax);

		Vector3 worldPosMin = new Vector3(obj.renderer.bounds.min.x, obj.renderer.bounds.min.y, 0);
		Vector3 screenPosMin = camera.WorldToScreenPoint(worldPosMin);
		//Debug.Log(screenPos.ToString());
		//Rect pos = new Rect(screenPos.x, screenPos.y, 10, 10);
		//Debug.Log("Pos: " + pos.x + ", x: " + Screen.width);

		if (screenPosMin.x > Screen.width || screenPosMax.x < 0)
			return false;
		return true;
	}

}
